package com.sfx.concurrent.demo.线程基础.线程的创建;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

public class LockSupportDemo {

    private static AtomicInteger atomicInteger = new AtomicInteger();

    public static void main(String[] args) {

    }






    public static class ChangeObjectThread extends Thread {
        public ChangeObjectThread(String name) {
            super(name);
        }

        @Override
        public void run() {
            System.out.println("即将进入无限时阻塞");
            //阻塞当前线程
            LockSupport.park();
            if (Thread.currentThread().isInterrupted()) {
                System.out.println("被中断了，但仍然会继续执行");
            } else {
                System.out.println("被重新唤醒了");
            }
        }
    }

    //LockSupport 测试用例
    public static void main1(String[] args) {
        ChangeObjectThread t1 = new ChangeObjectThread("线程一");
        ChangeObjectThread t2 = new ChangeObjectThread("线程二");
        //启动线程一
        t1.start();
        sleepSeconds(1);
        //启动线程二
        t2.start();
        sleepSeconds(1);
        //中断线程一
        t1.interrupt();
        //唤醒线程二
        LockSupport.unpark(t2);
    }

    public static void sleepSeconds(int seconds) {
        try {
            seconds = seconds * 1000;
            Thread.sleep(seconds);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}